#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dados.h"

dado* bd[1000];
int counter = 0;

dado* create_dado(char *locale, int number, char *date){
	dado* x = (dado*)malloc(sizeof(dado));

	x->locale = strdup(locale);
	x->number = number;
	x->date = strdup(date);

	return x;
}

void delete_dado(dado *d){
   	free(d->locale);
   	free(d->date);
	free(d);
	d = NULL;
}

void insert_dado(char *locale, int number, char *date){
	bd[counter] = create_dado(locale, number, date);
	counter++;
}

dado** get_bd(int *number){
	dado** d = bd;
	*number = counter;
	return d;
}

dado** find_dado_by_date(char *date, int *number){
	int i;
	int acks = 0;
	dado** aux;
	for(i = 0; i <= counter && bd[i] != NULL; i++){
		if(strcmp((bd[i])->date, date) == 0){
			acks++;
			aux = realloc(aux, acks * sizeof(dado*));
			aux[i] = bd[i];
		}
	}
	*number = acks;
	return aux;
}

dado** find_dado_by_locale(char *locale, int *number){
	int i;
	int acks = 0;
	dado** aux;
	for(i = 0; i <= counter && bd[i] != NULL; i++){
		if(strcmp((bd[i])->locale, locale) == 0){
			acks++;
			aux = realloc(aux, acks * sizeof(dado*));
			aux[i] = bd[i];
		}
	}
	*number = acks;
	return aux;
}

dado** find_dado_by_locale_and_date(char *locale, char *date, int *number){
	int i;
	int acks = 0;
	dado** aux;
	for(i = 0; i <= counter && bd[i] != NULL; i++){
		if((strcmp((bd[i])->locale, locale) == 0) && (strcmp((bd[i])->date, date) == 0)){
			acks++;
			aux = realloc(aux, acks * sizeof(dado*));
			aux[i] = bd[i];
		}
	}
	*number = acks;
	return aux;
}

void print_dado(dado* d){
	printf("------------------------");
	printf("locale: %s\n", d->locale);
	printf("number: %d\n", d->number);
	printf("date: %s\n", d->date);
	printf("++++++++++++++++++++++++");
}

void print_db(){
	int i;
	for(i = 0; i <= counter; i++){
		print_dado(bd[i]);
	}
}
